package com.beemans.corekit.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

public abstract class BaseFragment<VB extends ViewBinding> extends Fragment {
    protected VB binding;

    /**
     * 子类实现此方法，提供具体的 ViewBinding 对象
     */
    @Nullable
    protected abstract VB getViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container);

    protected void initData(@Nullable Bundle bundle) {
    }

    /**
     * 子类实现此方法，初始化视图和设置事件
     */
    protected abstract void initViews();

    /**
     * 初始化事件
     */
    protected void initEvents() {
    }

    /**
     * 加载数据
     */
    protected void loadData() {
    }

    /**
     * 初始化观察者
     */
    protected void initObverse() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = getViewBinding(inflater, container);
        return binding != null ? binding.getRoot() : null;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        initData(getArguments());
        initViews();
        initEvents();
        initObverse();
        loadData();
    }
}